<?php

namespace app\index\controller\gupiao;

use think\Controller;
use app\index\controller\Base;
use think\Db;


/**
 * 文章控制器
 */
class Article extends Base
{
	/**
	 * 文章信息
	 * URL :index/gupiao.article/info?id=1 
	 * @return [type] [description]
	 */
    public function info()
    {
        $id  	= input('id');
        if(!$id){
        	return json([
	                'code'  => 400,
	                'dest'  => '参数不完整'
	            ]);
        }

        $info = [];

        $info 	= Db('article')->where('id',$id)->find();
        if($info){
            $info['create_time']    = date('Y-m-d H:i:s',$info['create_time']);
            $info['update_time']    = date('Y-m-d H:i:s',$info['update_time']);
        	$re = Db('article')->where('id',$id)->setInc('num',1);
        }

        return json([
                'code'  => 200,
                'dest'  => 'success',
                'data'  => $info
            ]);

    }



    /**
     * 评论列表
     * URL :index/gupiao.article/talkList?id=1
     * @return [type] [description]
     */
    public function talkList()
    {
        $id     = input('id');
        if(!$id){
            return json([
                    'code'  => 400,
                    'dest'  => '参数不完整'
                ]);
        }

        $page       = input('page')     ? input('page')     : 1;                                   // 页码
        $pageSize   = input('limit')    ? input('limit')    : config('paginate')['list_rows'];     // 每页显示条数

        $talk   = Db('article_talk')
                ->where('article_id',$id)
                ->where('type',1)
                ->order('time desc')
                ->paginate(array('list_rows'=>$pageSize,'page'=>$page))
                ->toArray();

        if($talk['data']){
            foreach ($talk['data'] as $key => $value) {
                $talk['data'][$key]['time']   = date('Y-m-d H:i:s',$value['time']);
            }
            
        }

        return json([
                'code'      => 200,
                'dest'      => 'success',
                'count'     => $talk['total'],
                'data'      => $talk['data'],
                'is_manager'=> $this->admin,
                'uid'       => $this->uid
            ]);
    }



    /**
     * 用户评论
     * URL :index/gupiao.article/talk
     * @return [type] [description]
     */
    public function talk()
    {
    	$params['name']			= $this->username;
    	$params['avatar']		= $this->photo;
    	$params['uid']			= $this->uid;
    	$params['article_id']	= input('id');
    	$params['content'] 		= input('content');
    	$params['time']			= time();

    	if($params['article_id'] == null || $params['content'] == null){
    		return json([
	                'code'  => 400,
	                'dest'  => '参数不完整'
	            ]);
    	}

    	$re = Db('article_talk')->insert($params);
    	return json([
                'code'  => 200,
                'dest'  => '评论成功'
            ]);
    }


    /**
     * 未读回复
     * URL :index/gupiao.article/unRead
     * @return [type] [description]
     */
    public function unRead()
    {

    	if($this->uid<1){
    		return json([
	                'code'  => 401,
	                'dest'  => '未登录用户'
	            ]);
    	}

    	// 未读的已回复列表
    	$list = Db('article_talk')
            ->where('is_read',0)
            ->where('uid',$this->uid)
            ->where('type',2)
            ->select();
    	if($list){
            foreach ($list as $key => $value) {
                $list[$key]['time']     = date("Y-m-d H:i:s",$value['time']);
            }
    		return json([
	                'code'  => 200,
	                'dest'  => 'success',
	                'data'  => $list
	            ]);
    	}else{
    		return json([
	                'code'  => 400,
	                'dest'  => '暂无回复信息'
	            ]);
    	}
    	
    }



    /**
     * 查看未读回复
     * URL :index/gupiao.article/read
     * @return [type] [description]
     */
    public function read()
    {
    	$id = input('id');
    	if(!$id){
    		return json([
	                'code'  => 400,
	                'dest'  => '参数不完整'
	            ]);
    	}

    	// 回复的消息
    	$info['reply']     = Db('article_talk')->where('id',$id)->find();
        $info['reply']['time']      = date("Y-m-d H:i:s",$info['reply']['time']);

    	// 历史评论
    	$info['question']	= Db('article_talk')->where('id',$info['reply']['reply_id'])->find();
        $info['question']['time']   = date("Y-m-d H:i:s",$info['question']['time']);

    	// 标为已读
    	$re = Db('article_talk')->where('id',$id)->update(['is_read'=>1]);
    	return json([
	                'code'  => 200,
	                'dest'  => 'success',
                    'data'  => $info
	            ]);
    }




    /**
     * 管理员回复
     * url:index/gupiao.article/replyTalk
     * @return [type] [description]
     */
    public function replyTalk()
    {
        // 管理员权限验证
        if($this->admin != 1){
            return json([
                    'code'  => 401,
                    'dest'  => '权限不足'
                ]);
        }

        $data['reply_id']   = input('uid');
        $data['content']    = input('content');
        if(!$data['reply_id'] || $data['content'] == null){
            return json([
                    'code'  => 400,
                    'dest'  => '必填项不可为空'
                ]);
        }

        // 评论用户id
        $uid = Db('article_talk')->where('id',input('uid'))->value('uid');

        $data['name']       = $this->username;
        $data['avatar']     = $this->photo;
        $data['uid']        = $uid;
        $data['time']       = time();
        $data['type']       = 2;

        // 回复
        $re = Db('article_talk')->insert($data);

        // 更改被回复留言已读状态
        $re = Db('article_talk')->where('id',input('uid'))->update(['is_read'=>1]);

        return json([
                'code'  => 200,
                'dest'  => '回复成功'
            ]);


    } 
    


}
